package com.vcredit.creditcat.interceptor;

import com.vcredit.creditcat.security.SecurityHelper;
import com.vcredit.creditcat.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by liaoshengjun on 2017/8/28.
 */
public class PrivilegesInterceptor extends HandlerInterceptorAdapter{
	@Autowired
	private ResourceService resourceService;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String url=request.getRequestURI();
		String[] array=url.split("/");
		if(array.length<1){
			return true;
		}
		String value= array[array.length-1];
		int count=resourceService.findPrivilegesByValue(value);
		if(count==0){
			return true;
		}
		//查询按钮权限
		List<String> privileges=resourceService.findPrivilegesByUserId(SecurityHelper.getUserId(),1);
		if(privileges.size()==0){
			return false;
		}
		for(String privilege:privileges){
			if(value.equals(privilege)){
				return true;
			}
		}
		return false;
	}
}
